package org.originit.factory.core.io;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class ClassPathResource implements Resource{

    private final String path;

    private ClassLoader classLoader;

    public ClassPathResource(String path) {
        this(path,ClassPathResource.class.getClassLoader());
    }

    public ClassPathResource(String path, ClassLoader classLoader) {
        this.path = path;
        this.classLoader = classLoader;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        InputStream resource = classLoader.getResourceAsStream(this.path);
        if (resource == null) {
            throw new FileNotFoundException("resource not exist");
        }
        return resource;
    }
}
